import 'package:flutter/material.dart';

import '../config/index.dart';
import '../tools/index.dart';

/// 初始化MaterialApp
Widget initMaterialApp({
  Widget Function(BuildContext, Widget?)? builder,
  required ThemeData theme,
}) {
  return MaterialApp(
    /// 入口路由
    initialRoute: RouterS.configNoramlRouts(),

    /// 主题配置
    theme: theme,

    /// 暗黑主题配置
    darkTheme: themeDataDark,

    /// 所有路由集合
    routes: RouterS.getAllRoutS(),

    /// 是否显示 导航栏右上角 debug 标识
    debugShowCheckedModeBanner: false,

    /// 页面构建器
    builder: builder,
  );
}

Widget initGestureDetector(BuildContext context, Widget child) {
  return GestureDetector(
    onTap: () {
      FocusScope.of(context).requestFocus(FocusNode());
    },
    child: child,
  );
}

/// 初始化App
Widget initRunApp() {
  // 获取主题管理器实例
  final ThemeManager themeManager = ThemeManager();

  // 使用ValueListenableBuilder监听主题变化
  return ValueListenableBuilder<ThemeData>(
    valueListenable: themeManager,
    builder: (context, theme, _) {
      return initMaterialApp(
        theme: theme,
        builder: (context, child) {
          return initGestureDetector(context, child ?? Container());
        },
      );
    },
  );
}
